package com.jiyun.behavior.service.ipl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.jiyun.behavior.mapper.ApLikesBehaviorMapper;
import com.jiyun.behavior.service.ApLikesBehaviorService;
import com.usian.model.behavior.*;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ApLikesBehaviorServiceipl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior>implements ApLikesBehaviorService {

    @Resource
    private ApLikesBehaviorMapper apLikesBehaviorMapper;


    public ResponseResult dianzanxingwei(FollowBehaviorDto dto, ApBehaviorEntry apBehaviorEntry){

        if (dto==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Long articleId = dto.getArticleId();
        Integer followId = dto.getFollowId();
        Integer userId = dto.getUserId();
        ApLikesBehavior apLikesBehavior=new ApLikesBehavior();
        apLikesBehavior.setArticleId(articleId);
        apLikesBehavior.setCreatedTime(new Date());
        apLikesBehavior.setEntryId(apBehaviorEntry.getId());
        apLikesBehavior.setType(apBehaviorEntry.getType());
        apLikesBehavior.setOperation(1);
        apLikesBehaviorMapper.insert(apLikesBehavior);
        return  ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult dianzanxingweitu(LikesBehaviorDto dto) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
       ApLikesBehavior apLikesBehavior= apLikesBehaviorMapper.selectOne(Wrappers.<ApLikesBehavior>lambdaQuery().eq(ApLikesBehavior::getArticleId,dto.getArticleId()));
       if (apLikesBehavior!=null){
           if (apLikesBehavior.getType()==0){
               if (dto.getOperation()==1){
                   LambdaUpdateWrapper<ApLikesBehavior> qw=new LambdaUpdateWrapper<>();
                   qw.eq(ApLikesBehavior::getId,apLikesBehavior.getId());
                   qw.set(ApLikesBehavior::getOperation,1);
                   qw.set(ApLikesBehavior::getCreatedTime,new Date());
                   apLikesBehaviorMapper.update(apLikesBehavior,qw);
                   return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
               }else {
                   LambdaUpdateWrapper<ApLikesBehavior> qw=new LambdaUpdateWrapper<>();
                   qw.eq(ApLikesBehavior::getId,apLikesBehavior.getId());
                   qw.set(ApLikesBehavior::getOperation,0);
                   qw.set(ApLikesBehavior::getCreatedTime,new Date());
                   apLikesBehaviorMapper.update(apLikesBehavior,qw);
                   return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
               }

           }
       }else {
           ApLikesBehavior apLikesBehavior1=new ApLikesBehavior();
           apLikesBehavior1.setOperation(dto.getOperation());
           apLikesBehavior1.setEntryId(dto.getEquipmentId());
           apLikesBehavior1.setArticleId(dto.getArticleId());
           apLikesBehavior1.setType(dto.getType());
           apLikesBehaviorMapper.insert(apLikesBehavior1);
           return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);


       }






        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
